package iewShopApp.namespace;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.inputmethodservice.ExtractEditText;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

public class DrillActivity extends Activity {
	/** Called when the activity is first created. */
	private ImageButton HomeButton;
	public GlobalVars Constants;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.drill);
		
		Constants = new GlobalVars();
		Constants.drillsfm = 0;
		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		Spinner materialSpinner = (Spinner) findViewById(R.id.spinnerMaterial);
		materialSpinner.setSelection(4);

		SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		final EditText userMachine = (EditText) findViewById(R.id.MachineRPM);
		userMachine.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button

				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					// Perform action on key press
					String strValue = userMachine.getText().toString();
					Constants.maxRPM = Integer.parseInt(strValue);
					setSpeed();
					setFeed();
					// Force EditText from gaining focus all the bloody time
					userMachine.clearFocus();
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(userMachine.getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);

					return true;
				}
				return false;
			}

		});

		spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
		materialSpinner
				.setOnItemSelectedListener(new MyOnItemSelectedListener2());

		addListenerOnButton7();
	}

	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {
			TextView diameterDisplay = (TextView) findViewById(R.id.textDia);
			TextView peckDisplay = (TextView) findViewById(R.id.textMaxPeck);

			// TextView spindleDisplay = (TextView)
			// findViewById(R.id.textSpindleSpeed);
			RadioButton hssSelected = (RadioButton) findViewById(R.id.hssButton);
			RadioButton cobaltSelected = (RadioButton) findViewById(R.id.cobaltButton);
			TextView tipCompensationDisplay = (TextView) findViewById(R.id.tipCompensation);
			// TextView drillFeed = (TextView) findViewById(R.id.DrlFeed);
			double selectedVal = Double.parseDouble(getResources()
					.getStringArray(R.array.drill_double_in_array)[parent
					.getSelectedItemPosition()]);
			Constants.drillSize = selectedVal;
			// CharSequence RPMchar = MachineRPM.getText();
			// Constants.maxMachineRpm =
			// Integer.parseInt(MachineRPM.getText().toString());
			setSpeed();
			setFeed();
			diameterDisplay.setText(String.valueOf(Constants.drillSize));
			peckDisplay.setText(String.valueOf(Math
					.round(1000 * (Constants.drillSize) / 4) / 1000.));

			if (cobaltSelected.isChecked()) {
				tipCompensationDisplay.setText(String.valueOf(Math
						.rint(((Constants.drillSize) / (2 * Math.tan(Math
								.toRadians(135 / 2)))) * 10000) / 10000));
				// hssSelected.setChecked(false);
			}
			if (hssSelected.isChecked()) {
				tipCompensationDisplay.setText(String.valueOf(Math
						.rint(((Constants.drillSize) / (2 * Math.tan(Math
								.toRadians(118 / 2)))) * 10000) / 10000));
				// cobaltSelected.setChecked(false);
			}

			// parent.getItemAtPosition(pos).toString(),
			// Toast.LENGTH_LONG).show();
		}

		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}

	public class MyOnItemSelectedListener2 implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos,
				long id) {

			String sfmString = getResources().getStringArray(
					R.array.material_array_sfm)[parent
					.getSelectedItemPosition()];
			int sfmInt = Integer.parseInt(sfmString);

			// Constants.drillMaterial = materialInt;
			Constants.drillsfm = sfmInt;
			setSpeed();
			setFeed();

		}

		public void onNothingSelected(AdapterView parent) {
			// Do nothing.
		}
	}

	public void onRadioButtonClicked(View v) {
		// Perform action on clicks

		RadioButton hssSelected = (RadioButton) findViewById(R.id.hssButton);
		RadioButton cobaltSelected = (RadioButton) findViewById(R.id.cobaltButton);
		TextView tipCompensationDisplay = (TextView) findViewById(R.id.tipCompensation);
		RadioButton rb = (RadioButton) v;
		setSpeed();
		setFeed();
		if (rb.getText().equals("Cobalt")) {
			tipCompensationDisplay.setText(String.valueOf(Math
					.rint(((Constants.drillSize) / (2 * Math.tan(Math
							.toRadians(135 / 2)))) * 10000) / 10000));
			hssSelected.setChecked(false);
		} else {
			tipCompensationDisplay.setText(String.valueOf(Math
					.rint(((Constants.drillSize) / (2 * Math.tan(Math
							.toRadians(118 / 2)))) * 10000) / 10000));
			cobaltSelected.setChecked(false);
		}

	}

	public void addListenerOnButton7() {
		/*HomeButton = (ImageButton) findViewById(R.id.HomeClick);
		HomeButton.setOnClickListener(new OnClickListener() {

			// @Override
			public void onClick(View arg0) {
				finish();

			}
		}); */
	}

	public void setSpeed() {
		TextView spindleDisplay = (TextView) findViewById(R.id.textSpindleSpeed);
		Constants.drillRPM = Math.min(
				Math.round(Constants.drillsfm * 12
						/ ((Constants.drillSize) * Math.PI)), Constants.maxRPM);
		spindleDisplay.setText(String.valueOf((int) Constants.drillRPM));
	}

	public void setFeed() {
		TextView drillFeed = (TextView) findViewById(R.id.DrlFeed);
		// double RPM = Math.round(Constants.drillsfm * 12
		// / (Double.parseDouble(Constants.drillSize) * Math.PI));

		drillFeed.setText(String.valueOf(Math.round(Constants.drillRPM * .01
				* (Constants.drillSize) / (.0625)) / 10.));
		// drillFeed.setText(String.valueOf(RPM));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.textMill:
			setContentView(R.layout.mill);
			addListenerOnButton7();
			break;
		case R.id.textDrill:
			finish();
			Intent myIntent1 = new Intent().setClass(this, DrillActivity.class);
			startActivity(myIntent1);
			// setContentView(R.layout.drill);

			addListenerOnButton7();
			break;
		case R.id.textLathe:
			setContentView(R.layout.lathe);
			addListenerOnButton7();
			break;
		case R.id.textReam:
			setContentView(R.layout.ream);
			addListenerOnButton7();
			break;
		case R.id.textTap:
			finish();
			Intent myIntent2 = new Intent().setClass(this, TapActivity.class);
			startActivity(myIntent2);
			addListenerOnButton7();
			break;
		case R.id.textLearning:
			// trial = new LearningActivity();
			// trial.update();
			setContentView(R.layout.learning);
			// defineLearningButtons();
			// addListenerOnButton();
			// addListenerOnButton2();
			// addListenerOnButton3();
			// addListenerOnButton4();
			// addListenerOnButton5();
			// addListenerOnButton6();
			addListenerOnButton7();
			break;

		}
		return true;
	}
}
